<?php

namespace app\common\model\coupon;

use think\Model;

class Coupon extends Model
{
    protected $name = 'coupon';

    //定义数据库里不存在的数据
    protected $append = [
        'thumb_cdn',
        'createtime_text',
        'status_status',
        'endtime_text'
    ];


    public function getThumbCdnAttr($value, $data)
    {
        // 获取当前域名
        $domain = request()->domain();

        $thumb = $data['thumb'] ?? '';

        if (!$thumb || is_file('.'.$thumb)) {
            return $domain.'/assets/img/coupon.jpg';
        }
        return $domain.$thumb;
    }


    public function getCreateTimeTextAttr($value,$data)
    {
        $createTime = empty($data['createtime']) ? '' : $data['createtime'];

        if (empty($createTime)) {
            return '未知时间';
        }

        return date('Y-m-d', $createTime);
    }


    public function getEndTimeTextAttr($value,$data){
        $endTime = empty($data['endtime']) ? '' : $data['endtime'];

        if (empty($endTime)) {
            return '未知时间';
        }

        return date('Y-m-d', $endTime);
    }


    public function  getStatusStatusAttr($value,$data){
        $status = empty($data['status']) ? 0 : $data['status'];
        $statusList = $this->statusList();
        return $statusList[$status];
    }
    public function statusList(){
        return [
            '0' => _("活动结束"),
            '1' => _("活动进行中")
        ];
    }
}
